home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Applications / Calculators / NumberCrunch 1.41 / Number Crunch II Demo / Writing External Codes (xCOD) / Pascal source code / SAMPLE_XCOD.p < prev    next >
Encoding:
Text File  |  1992-09-03  |  4.4 KB  |  131 lines  |  [TEXT/PJMM]

  1. {This is Pascal source code which will compile into}
  2. {an xCOD (eXternal CODe) resource for NumberCrunch II.}
  3. {}
  4. {All variables passed MUST be declared var and }
  5. {MUST be extended or array of extended or external procedures.}
  6. {}
  7. { After building this code resource you must install it into NumberCrunch with either ResEdit or the}
  8. { Load xCOD command from inside NCII. }
  9. {}
  10. { In either case, an sCOD string or resource is also needed to tell NCII what the arguments to this routine are.}
  11. {     For this routine, the appropriate sCOD is:}
  12. {}
  13. {    xCOD xSAMPLE_XCODE(J:num; Bj:Array; prog Foo(K:num; Ak:array); Cj:ComplexArray), sets all of Cj[1…J] to 1 + 0i,  Bj[1…j] to 1…j, and passes a K=3 element array [3,2,1] to Foo.}
  14. {}
  15. {   The procedure arguments are}
  16. {           J        :   extended number}
  17. {            Bj        :   array of extended}
  18. {            Foo        :   the name of another xCOD or an NCII user program }
  19. {        Cj      :   an array of complex numbers, i.e. a 2N array of extended.}
  20. {}
  21. {}
  22. { •••••••••••••••••}
  23. { • SAMPLE_XCOD       •}
  24. { •••••••••••••••••}
  25. {}
  26. { This xCOD just shows how to pass complex quantities and external routines in Pascal.}
  27. { To incorporate this into a Project, }
  28. {   • include the files}
  29. {        DRVRRuntime.lib}
  30. {        Interface.lib}
  31. {        SANELib.lib}
  32. {        SANE.p}
  33. {          this file,  SAMPLE_XCOD.p}
  34. {   • In Set Project Type under the Project menu, set}
  35. {          Code Resource, }
  36. {          File:        Type= 'rsrc';  Creator='RSED'}
  37. {          Resource:  Name = 'SAMPLE_XCOD',  Type='xCOD', }
  38. {                        ID=42 (or any unique ID.)}
  39. {         }
  40. {  }
  41. unit SAMPLE_XCOD;
  42. interface
  43.     uses
  44.         SANE;
  45.  
  46.     type
  47.     { Array of extended numbers. The 1..1 range just ensures that no range}
  48.     { checking is done. If you write outside the range that is reserved by the}
  49.     { calling routine, the system will probably crash.}
  50.     {}
  51.         ExtendedArrayType = array[1..1] of extended;
  52.  
  53.     { Pascal does not support complex numbers, so usually it's easier to}
  54.     { treat complex arrays as ExtendedArrayType which are packed with}
  55.     { real and complex parts alternating, i.e. a 2N array (r1,c1,r2,c2,r3,c3...,rN,cN). }
  56.     { However, one could use these types for pascal complex numbers  a.r, a.c and }
  57.     { complex arrays b[j].r and b[j].c.   }
  58.     {}
  59.         ComplexNumberType = record
  60.                 r, c: extended;
  61.             end;
  62.         ComplexArrayType = array[1..1] of ComplexNumberType;
  63.  
  64. {   The procedure arguments are}
  65. {           eJ        :   extended number}
  66. {            Bj        :   array of extended}
  67. {            Foo        :   the name of another xCOD or an NCII user program }
  68. {        Cj    :   an array of complex numbers, i.e. a 2N array of extended.}
  69.     procedure Main (var eJ: extended; {}
  70.                                     var Bj: ExtendedArrayType; {}
  71.                                     FooPtr: Ptr; {}
  72.                                     var Cj: ExtendedArrayType);
  73.  
  74. implementation
  75.  
  76.  
  77. { To pass an array of 3 extended numbers to the external procedure Foo, }
  78. { we must have an actual variable of that length. }
  79. { We cannot use ExtendedArrayType, because it is only declared to be }
  80. { of size [1..1], which avoids any range checking on passed variables.}
  81.     const
  82.         OtherSize = 3;  {The size of the array to be passed to Foo}
  83.     type
  84.         ThreeExtendedType = array[1..OtherSize] of extended;
  85.  
  86.  
  87. {This procedure must have arguments that match the declaration of }
  88. {the program "foo" in the sCOD line above, with the "address" argument added.}
  89. {The inline code here just strips the "address" from the argument stack and }
  90. {jumps to Address, which is the address of the real foo routine passed as a Pointer.}
  91. {}
  92. { Note that since the array that will be sent is of ThreeExtendedType rather}
  93. { than the vanilla ExtendedArrayType, that is how "a" is declared here.}
  94. {}
  95.     procedure fooInterface (var n: extended; var a: ThreeExtendedType; {}
  96.                                     address: Ptr);
  97.     inline
  98.         $205F, $4E90;
  99.  
  100.     procedure Main;
  101.         var
  102.             J, i: integer;
  103.             anArrayForFoo: ThreeExtendedType;
  104.             SizeForFoo: extended;
  105.     begin
  106.         J := Num2Integer(eJ);  { This is a SANE routine that converts extended to integer }
  107.  
  108. { Set the elements of Bj to 1,2,3,… }
  109.         for i := 1 to J do
  110.             Bj[i] := 1.0 * i;
  111.  
  112. { Set all the real parts of Cj to 1, and the imaginary parts to 0.}
  113.         for i := 1 to J do
  114.             begin
  115.                 Cj[2 * i - 1] := 1.0;    {real parts}
  116.                 Cj[2 * i] := 0.0;          {complex parts}
  117.             end;
  118.  
  119.         AnArrayForFoo[1] := 3.0;    { Set up the array to pass to Foo.}
  120.         AnArrayForFoo[2] := 2.0;
  121.         AnArrayForFoo[3] := 1.0;
  122.         SizeForFoo := OtherSize;  { This size must be passed as an extended, so we put it into a variable of that type.}
  123. {Call the routine Foo }
  124.         FooInterface(SizeForFoo, AnArrayForFoo, FooPtr);
  125.  
  126. { That's it. }
  127.  
  128.     end; {Main}
  129.  
  130.  
  131. end.